package com.shop.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.shop.context.BaseContext;
import com.shop.dto.CommentPageQueryDTO;
import com.shop.dto.CommentsDTO;
import com.shop.entity.Comments;
import com.shop.entity.CommentsReply;
import com.shop.entity.Employee;
import com.shop.mapper.CommentsMapper;
import com.shop.mapper.CommentsReplyMapper;
import com.shop.mapper.EmployeeMapper;
import com.shop.result.PageResult;
import com.shop.result.Result;
import com.shop.service.CommentsService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class CommentsServiceImpl implements CommentsService {

    @Autowired
    private CommentsMapper commentsMapper;
    @Autowired
    private CommentsReplyMapper commentsReplyMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    /**
     * 增加评论
     * @param comments
     */
    @Override
    public void save(Comments comments) {
        Long fromId = BaseContext.getCurrentId();
        comments.setFromId(fromId);
        comments.setCreateTime(LocalDateTime.now());
        //设置商铺默认id为1
        comments.setOwnerId(1);
        commentsMapper.save(comments);
    }

    /**
     * 根据商户id查询评论
     * @param ownerId
     * @return
     */
    @Override
    public List<CommentsDTO> getById(Long ownerId) {
        List<Comments> comments = commentsMapper.getById(ownerId);
        List<CommentsDTO> commentsDTOList = new ArrayList<>();

        for (Comments comment : comments) {
            CommentsDTO dto = new CommentsDTO();
            // 映射评论信息到DTO
            BeanUtils.copyProperties(comment, dto);

            // 查询一条回复
            CommentsReply reply = commentsReplyMapper.getById(comment.getId());
            dto.setCommentsReply(reply);

            commentsDTOList.add(dto);
        }

        return commentsDTOList;
    }

    /**
     * 删除评论
     * @param id
     * @return
     */
    @Override
    @Transactional
    public void delete(Long id) {
        commentsMapper.delete(id);
        commentsReplyMapper.delete(id);
    }

    /**
     * 增加回复
     * @param commentsReply
     */
    @Override
    public void saveReply(CommentsReply commentsReply) {
        commentsReply.setCreateTime(LocalDateTime.now());
        commentsReply.setToId(BaseContext.getCurrentId());
        Employee employee = employeeMapper.getById(BaseContext.getCurrentId());
        commentsReply.setToName(employee.getName());
        commentsReplyMapper.saveReply(commentsReply);
    }

    /**
     * 查询评论
     * @param commentsDTOs
     * @return
     */
    @Override
    public List<CommentsDTO> list(List<CommentsDTO> commentsDTOs) {
        List<Comments> comments = commentsMapper.list();
        List<CommentsDTO> commentsDTOList = new ArrayList<>();

        for (Comments comment : comments) {
            CommentsDTO dto = new CommentsDTO();
            // 映射评论信息到DTO
            BeanUtils.copyProperties(comment, dto);

            // 查询一条回复
            CommentsReply reply = commentsReplyMapper.getById(comment.getId());
            dto.setCommentsReply(reply);

            commentsDTOList.add(dto);
        }
        return commentsDTOList;
    }

    @Override
    public PageResult pageQuery(CommentPageQueryDTO commentPageQueryDTO) {

        //select * from employee limt 0,10
        //开始分页查询
        PageHelper.startPage(commentPageQueryDTO.getPage(),commentPageQueryDTO.getPageSize());

        Page<Comments> page = commentsMapper.pageQuery(commentPageQueryDTO);

        long total = page.getTotal();
        List<Comments> records = page.getResult();
        return new PageResult(total,records);
    }
}
